package com.sc.nft.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sc.nft.dao.PrizeDrawActivityAwardLogDao;
import com.sc.nft.entity.PrizeDrawActivityAwardLog;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.dto.ActivityLogDTO;
import com.sc.nft.entity.vo.RollVO;
import com.sc.nft.service.PrizeDrawActivityAwardLogService;
import com.sc.nft.sup.PageRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import sun.text.CollatorUtilities;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 抽奖活动的奖品日志(PrizeDrawActivityAwardLog)表服务实现类
 *
 * @author wwc
 * @since 2023-08-23 15:35:32
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class PrizeDrawActivityAwardLogServiceImpl implements PrizeDrawActivityAwardLogService {

    private final PrizeDrawActivityAwardLogDao prizeDrawActivityAwardLogDao;

    @Override
    public IPage<PrizeDrawActivityAwardLog> activityLog(ActivityLogDTO activityLogDTO, UserInfo userInfo) {
        return prizeDrawActivityAwardLogDao.activityLog(activityLogDTO, userInfo);
    }

    @Override
    public List<RollVO> roll(Long id) {
        List<RollVO> list = new ArrayList<>();
        // 获取排名前三的数据三条
        List<PrizeDrawActivityAwardLog> threeSort = prizeDrawActivityAwardLogDao.getThreeSort(id);
        // threeSort的id转换为一个集合
        List<Long> ids = threeSort.stream().map(PrizeDrawActivityAwardLog::getId).collect(Collectors.toList());
        // 获取排名最新的10条数据
        List<PrizeDrawActivityAwardLog> prizeDrawActivityAwardLogs = prizeDrawActivityAwardLogDao.newestTen(id, ids);
        // prizeDrawActivityAwardLogs和threeSort合并
        threeSort.addAll(prizeDrawActivityAwardLogs);
        //  让后组装成  恭喜XXX在转盘抽奖活动中抽中XXX
        for (PrizeDrawActivityAwardLog prizeDrawActivityAwardLog : threeSort) {
            RollVO rollVO = new RollVO();
            String content = StrUtil.format("恭喜{}抽中{}", prizeDrawActivityAwardLog.getNickName(), prizeDrawActivityAwardLog.getAwardName());
            rollVO.setContent(content);
            list.add(rollVO);
        }
        return list;
    }

}
